package com.interview03;

class ListNode{
    int val;//节点数据
    ListNode next;//夏邑县 下一个节点的引用
    public ListNode(int val){
        this.val = val;
    }
}
public class Demo4 {
    public static ListNode FindKthToTail(ListNode head, int k) {
        if (head == null || k < 0) {
            return null;
        }
        ListNode last = head;
        for (int i = 1; i < k; i++) {
            if (head.next != null) {
                head = head.next;
            } else
                return null;
        }
        while (head.next != null) {
            head = head.next;
            last = last.next;
        }
        return last;
    }
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(6);
        int k = 3;
        ListNode last = FindKthToTail(head, k);
        System.out.println(last.val);
    }

    /*public static void main(String[] args){
        //构造方法  CylicBarrier(int parties,Runnabel barrierAction)
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,() -> {System.out.println("召唤神龙***");});

        for (int i = 1; i <= 7 ; i++)
        {
            final int tempInt = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"\t收集到第"+tempInt+"颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }

    }*/
}
